package com.lhl.forum.service.impl;

import com.lhl.forum.common.AppResult;
import com.lhl.forum.common.ResultCode;
import com.lhl.forum.dao.BoardMapper;
import com.lhl.forum.exception.ApplicationException;
import com.lhl.forum.model.Board;
import com.lhl.forum.service.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {

    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        //非空校验
        if (num <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_TWO_PWD_NOT_SAME));
        }

        //调用dao层
        List<Board> boards = boardMapper.selectByNum(num);

        return boards;
    }

    @Override
    public Board selectById(Long id) {
        //非空校验
        if (id <= 0 || id == null){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        //查询对应的版块
        Board board = boardMapper.selectByPrimaryKey(id);

        return board;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        //非空校验
        if ( id == null || id <= 0  ){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        //根据id查询版块
        Board board = boardMapper.selectByPrimaryKey(id);

        //对查询到的版块进行非空校验
        if (board == null){
            //打印日志
            log.warn(ResultCode.ERROR_IS_NULL.toString() + ", board id = " + id);
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }

        //更新版块的帖子数量
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);

        //对返回结果进行校验
        if (row != 1){
            //打印日志
            log.warn(ResultCode.FAILED.toString() + "受影响行数不等于1");
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

    }

    @Override
    public void subOneArticleCountById(Long id) {
        //非空校验
        if (id == null || id <= 0) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //获取版块信息
        Board board = boardMapper.selectByPrimaryKey(id);
        //判断版块是否存在
        if (board == null){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //判断版块状态
        if (board.getState() == 1){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_BANNED));
        }

        Board updateBoard = new Board();
        updateBoard.setId(id);
        updateBoard.setArticleCount(board.getArticleCount() - 1);

        //判断修改后帖子的数量是否小于0
        if (updateBoard.getArticleCount() < 0){
            updateBoard.setArticleCount(0);
        }

        //更新版块的帖子数量
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);

        //判断返回结果是否正常
        if (row != 1){
            //打印日志
            log.warn(ResultCode.FAILED.toString() + "受影响行数不等于1");
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }


    }
}
